/*
 * Copyright 2014-2025 Sayi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.deepoove.poi.data;

import static com.deepoove.poi.util.ByteUtils.startsWith;

/**
 * @author Sayi
 */
public enum AttachmentType {
    DOCX(
        new String[]{"504B0304", "DOCF11E0"},
        "Word.Document.12",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        new String[]{".docx", ".doc"},
        ""),
	XLSX(
        new String[]{"504B0304", "DOCF11E0"},
        "Excel.Sheet.12",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        new String[]{".xlsx", ".xls"},
        ""),
    ;

    private final String[] fileMagics;
    private final String programId;
    private final String contentType;
    private final String[] extensions;
    private final String icon;

    AttachmentType(String[] fileMagics, String programId, String contentType, String[] extensions, String icon) {
        this.fileMagics = fileMagics;
        this.programId = programId;
        this.contentType = contentType;
        this.extensions = extensions;
        this.icon = icon;
    }

    public String programId() {
        return programId;
    }

    public String contentType() {
        return contentType;
    }

    public String[] extensions() {
        return extensions;
    }

    public String ext() {
        return extensions[0];
    }

    public String icon() {
        return icon;
    }

    public static AttachmentType suggestFileType(byte[] bytes) {
        for (AttachmentType type : values()) {
            for (String magic : type.fileMagics) {
                if (startsWith(bytes, magic.getBytes())) {
                    return type;
                }
            }
        }
        return null;
    }

    public static AttachmentType suggestFileType(String fileLocation) {
        if (null == fileLocation) return null;
        for (AttachmentType type : values()) {
            for (String extension : type.extensions) {
                if (fileLocation.endsWith(extension)) {
                    return type;
                }
            }
        }
        return null;
    }
}
